package wqh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import wqh.domain.Device;
import wqh.domain.Role;
import wqh.global.GlobalDevice;
import wqh.global.GlobalRole;
import wqh.global.GlobalSession;
import wqh.service.AlarmEventConfirmService;
import wqh.service.AlarmEventService;
import wqh.service.DeviceService;
import wqh.service.RoleService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/water/view")
public class ViewController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private AlarmEventService alarmEventService;

    @Autowired
    private AlarmEventConfirmService alarmEventConfirmService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request)
    {
        ModelAndView modelAndView = new ModelAndView();
        List<Device> deviceList = deviceService.findAllRecord();
        Integer end = (int)(System.currentTimeMillis() / 1000);
        Integer start = end - (60*60*24*30);
        int monthAlarm = alarmEventService.countByTimeRange(start,end) + alarmEventConfirmService.countByTimeRange(start,end);
        int none_confirm_alarm_event = alarmEventService.selectCount();
        for(Device device : deviceList)
        {
            device.setPicture(request.getContextPath() + "/water/file/device/getPicture?id=" + device.getId());
            if(GlobalDevice.exist(device.getDevice_ip()))
            {
                device.setOnline((byte)1);
            }
            else
            {
                device.setOnline((byte)0);
            }
        }
        modelAndView.addObject("month_alarm_event",monthAlarm);
        modelAndView.addObject("none_confirm_alarm_event",none_confirm_alarm_event);
        modelAndView.addObject("deviceList",deviceList);
        modelAndView.addObject("li_class","1-0");
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.setViewName("index_ftl");
        return modelAndView;
    }

    @RequestMapping("/analysis_report")
    public ModelAndView analysis_report(HttpServletRequest request)
    {
        Role role = roleService.findByName(GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        int role_type = 0;
        if(role != null) role_type = role.getRole_auth_type_id();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role_type_id",role_type);
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","2-7");
        modelAndView.setViewName("analysis_report_ftl");
        return modelAndView;
    }

    @RequestMapping("/device")
    public ModelAndView device(HttpServletRequest request)
    {
        Role role = roleService.findByName(GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        int role_type = 0;
        if(role != null) role_type = role.getRole_auth_type_id();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("role_type_id",role_type);
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","2-1");
        modelAndView.setViewName("device_ftl");

        return modelAndView;
    }

    @RequestMapping("/channel")
    public ModelAndView channel(HttpServletRequest request)
    {
        Role role = roleService.findByName(GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        int role_type = 0;
        if(role != null) role_type = role.getRole_auth_type_id();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role_type_id",role_type);
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","2-2");
        modelAndView.setViewName("channel_ftl");

        return modelAndView;
    }

    @RequestMapping("/real_time_data")
    public ModelAndView real_time_data(HttpServletRequest request)
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","2-3");
        modelAndView.setViewName("real_time_data_ftl");
        return modelAndView;
    }

    @RequestMapping("/history_data")
    public ModelAndView history_data(HttpServletRequest request)
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","2-4");
        modelAndView.setViewName("history_data_ftl");
        return modelAndView;
    }

    @RequestMapping("/map_show")
    public ModelAndView map_show(HttpServletRequest request)
    {
        Role role = roleService.findByName(GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        int role_type = 0;
        if(role != null) role_type = role.getRole_auth_type_id();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role_type_id",role_type);
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","2-6");
        modelAndView.setViewName("map_ftl");
        return modelAndView;
    }

    @RequestMapping("/location")
    public ModelAndView location(HttpServletRequest request)
    {
        Role role = roleService.findByName(GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        int role_type = 0;
        if(role != null) role_type = role.getRole_auth_type_id();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role_type_id",role_type);
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","2-8");
        modelAndView.setViewName("location_ftl");
        return modelAndView;
    }

    @RequestMapping("/alarm_event")
    public ModelAndView alarm_event(HttpServletRequest request)
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","2-5");
        modelAndView.setViewName("alarm_event_ftl");
        return modelAndView;
    }

    @RequestMapping("/user_manage")
    public ModelAndView user_manage(HttpServletRequest request)
    {
        String roleName = GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        Role role = roleService.findByName(roleName);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("role_name", roleName);
        if(role != null) modelAndView.addObject("role_id",role.getId());
        modelAndView.addObject("li_class","2-9");
        modelAndView.setViewName("user_manage_ftl");
        return modelAndView;
    }

    @RequestMapping("/buy_model")
    public ModelAndView buy_model(HttpServletRequest request)
    {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.setViewName("buy_model_ftl");
        return modelAndView;
    }

    @RequestMapping("/dt_map_show")
    public ModelAndView dt_map_show(HttpServletRequest request)
    {
        Role role = roleService.findByName(GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        int role_type = 0;
        if(role != null) role_type = role.getRole_auth_type_id();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role_type_id",role_type);
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","3-0");
        modelAndView.setViewName("dt_map_show_ftl");
        return modelAndView;
    }

    @RequestMapping("/dt_alarm_event")
    public ModelAndView dt_alarm_event(HttpServletRequest request)
    {
        Role role = roleService.findByName(GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        int role_type = 0;
        if(role != null) role_type = role.getRole_auth_type_id();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role_type_id",role_type);
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","3-1");
        modelAndView.setViewName("dt_alarm_event_ftl");
        return modelAndView;
    }

    @RequestMapping("/dt_device")
    public ModelAndView dt_device(HttpServletRequest request)
    {
        Role role = roleService.findByName(GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        int role_type = 0;
        if(role != null) role_type = role.getRole_auth_type_id();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role_type_id",role_type);
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","3-3");
        modelAndView.setViewName("dt_device_ftl");
        return modelAndView;
    }


    @RequestMapping("/dt_analysis_report")
    public ModelAndView dt_analysis_report(HttpServletRequest request)
    {
        Role role = roleService.findByName(GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        int role_type = 0;
        if(role != null) role_type = role.getRole_auth_type_id();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role_type_id",role_type);
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","3-4");
        modelAndView.setViewName("dt_analysis_report_ftl");
        return modelAndView;
    }

    @RequestMapping("/dt_location")
    public ModelAndView dt_location(HttpServletRequest request)
    {
        Role role = roleService.findByName(GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        int role_type = 0;
        if(role != null) role_type = role.getRole_auth_type_id();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role_type_id",role_type);
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","3-5");
        modelAndView.setViewName("dt_location_ftl");
        return modelAndView;
    }

    @RequestMapping("/dt_monitor")
    public ModelAndView dt_monitor(HttpServletRequest request)
    {
        Role role = roleService.findByName(GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        int role_type = 0;
        if(role != null) role_type = role.getRole_auth_type_id();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role_type_id",role_type);
        modelAndView.addObject("user_name", GlobalSession.getUserNameBySessionID(request.getSession().getId()));
        modelAndView.addObject("role_name", GlobalRole.getRoleNameByUserName(GlobalSession.getUserNameBySessionID(request.getSession().getId())));
        modelAndView.addObject("user_picture",request.getContextPath() + "/water/file/user/getPicture");
        modelAndView.addObject("li_class","3-6");
        modelAndView.setViewName("dt_monitor_ftl");
        return modelAndView;
    }
}
